package exercise.chapter9.ex9_2;

public class LetterPrinter implements Runnable
{
	private StringBuffer sb;

	public LetterPrinter(StringBuffer aSb)
	{
		this.sb = aSb;
	}

	@Override
	public void run()
	{
		try
		{
			output();
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}

	private synchronized void output() throws InterruptedException
	{
		int counter = 0;
		while (counter < 100)
		{
			String s = String.format("[%s] -> %d:%s", Thread.currentThread().getName(), counter, sb.toString());
			System.out.println(s);
			counter++;
		}
		if (counter == 100)
		{
			char letter = sb.charAt(0);
			letter = (char) (letter + 1);
			sb.replace(0, 1, new Character(letter).toString());
		}
		Thread.sleep(50);
	}
}
